package com.ocom.accesservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.accesservice.dao.IDaoGroupInfo;
import com.ocom.accesservice.service.GroupInfoService;
import com.ocom.common.entity.access.EntityMjGroupInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.StringUtils;
import com.ocom.common.vo.DelAreaVo;
import com.ocom.common.vo.GroupListVo;
import com.ocom.common.vo.access.GroupVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class GroupInfoServiceImpl extends ServiceImpl<IDaoGroupInfo, EntityMjGroupInfo> implements GroupInfoService {


    @Autowired
    private IDaoGroupInfo daoGroupInfo;



    /**
     * 移除区域
     * */
    @DataSourceAspect(type= DataSourceEnum.PART_DB,companyId = "companyID",isMaster = MasterSlaveEnum.MASTER)
    @Transactional
    @Override
    public void RemoveArea(DelAreaVo delAreaVo, Long companyID) {
        QueryWrapper<EntityMjGroupInfo> query = Wrappers.query();
        if (StringUtils.isNotNull(delAreaVo.getComId())){
            query.eq("com_id",delAreaVo.getComId());
        }
        if (StringUtils.isNotNull(delAreaVo.getAreaId())){
            query.eq("area_id",delAreaVo.getAreaId());
        }
        daoGroupInfo.delete(query);

    }

    /**
     * 初始化单位门禁参数
     * */
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Transactional
    @Override
    public void InitCompanyPara(EntityMjGroupInfo entityMjGroupInfo,Long companyId) {

        daoGroupInfo.insert(entityMjGroupInfo);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<GroupListVo> getGroupList(Long companyId, Long areaNo) {
        QueryWrapper<EntityMjGroupInfo> queryWrapper=new QueryWrapper<>();

        queryWrapper.eq("status",0);
        queryWrapper.eq("com_id",companyId);
        queryWrapper.and(wrapper -> {
            wrapper.and(itemWrapper ->{
                itemWrapper.lt("group_no", 17);
                itemWrapper.eq("area_id", 0);
            });
            wrapper.or(itemWrapper ->{
                itemWrapper.gt("group_no", 16);
                itemWrapper.eq("area_id", areaNo);
            });
        });
        queryWrapper.orderByAsc("group_no");
        List<EntityMjGroupInfo> list=this.list(queryWrapper);
        List<GroupListVo> res=new ArrayList<>();
        if(list.size()>0){
            for (EntityMjGroupInfo entityMjGroupInfo:list){
                GroupListVo groupListVo=new GroupListVo();
                groupListVo.setRoleI(entityMjGroupInfo.getGroupNo());
                groupListVo.setGroupName(entityMjGroupInfo.getGroupName());
                res.add(groupListVo);
            }
        }
        return res;
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityMjGroupInfo> getGroupList(Long companyId) {
        QueryWrapper<EntityMjGroupInfo> query = Wrappers.query();
        query.eq("com_id",companyId);
        query.eq("status",0);
        return daoGroupInfo.selectList(query);
    }

    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityMjGroupInfo getGroupInfoById(Long companyId, Long roleI,Long areaNo) {
        QueryWrapper<EntityMjGroupInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("group_no",roleI);
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("area_id",areaNo);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    //查权限组编号或名称是否已使用：
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityMjGroupInfo getAccessGroup(Long companyId, GroupVo groupVo) {
        QueryWrapper<EntityMjGroupInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("area_id",groupVo.getAreaNo());
        if (groupVo.getGroupNo() !=null){
            queryWrapper.eq("group_no",groupVo.getGroupNo());
        }else {
            queryWrapper.eq("group_name",groupVo.getGroupName());
        }
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    //添加权限组
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public Integer addGroup(Long companyId, GroupVo groupVo) {
        EntityMjGroupInfo entityMjGroupInfo=new EntityMjGroupInfo();
        entityMjGroupInfo.setComId(groupVo.getCompanyID());
        entityMjGroupInfo.setGroupNo(groupVo.getGroupNo());
        entityMjGroupInfo.setGroupName(groupVo.getGroupName());
        entityMjGroupInfo.setAreaId(Long.valueOf(groupVo.getAreaNo()));
        entityMjGroupInfo.setStatus(0L);
        Date date=new Date();
        entityMjGroupInfo.setCreateTime(date);
        entityMjGroupInfo.setUpdateTime(date);
        return daoGroupInfo.insert(entityMjGroupInfo);
    }

    //添加权限组
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityMjGroupInfo selectName(Long companyId, GroupVo groupVo) {
        QueryWrapper<EntityMjGroupInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("area_id",groupVo.getAreaNo());
        queryWrapper.ne("group_no",groupVo.getGroupNo());
        queryWrapper.eq("group_name",groupVo.getGroupName());

        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    //修改权限组名称
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    @Override
    public Boolean updateGroup(Long companyId, GroupVo groupVo) {
        UpdateWrapper<EntityMjGroupInfo> updateWrapper=new UpdateWrapper<>();
        updateWrapper.set("group_name",groupVo.getGroupName());
        updateWrapper.set("update_time",new Date());
        updateWrapper.eq("com_id",groupVo.getCompanyID());
        updateWrapper.eq("group_no",groupVo.getGroupNo());
        updateWrapper.eq("area_id",groupVo.getAreaNo());
        return this.update(updateWrapper);
    }
}
